package com.gjy.spring.boot3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.support.ResourcePropertySource;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-12-18 12:47:44
 */
public class Boot5App {

    public static void main(String[] args) throws Exception {
        SpringApplication app = new SpringApplication();
        StandardEnvironment env = new StandardEnvironment();
        env.getPropertySources().addLast(new ResourcePropertySource("step6", "classpath:step6.properties"));
        // 环境变量中的键值与Java对象绑定
        // User user = Binder.get(env).bind("user", User.class).get();
        /*User user = new User();
        Binder.get(env).bind("user", Bindable.ofInstance(user));*/
        System.out.println(app);
        Binder.get(env).bind("spring.main", Bindable.ofInstance(app)); // debug
        System.out.println(app);
    }

    private static class User {
        private String firstName;
        private String lastName;

        @Override
        public String toString() {
            return "User{" +
                    "firstName='" + firstName + '\'' +
                    ", lastName='" + lastName + '\'' +
                    '}';
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    }
}
